/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.venta;

import Model.ModeloM;
import Util.ArrayListTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 *
 * @author Maryluz
 */
public class frmCantidad extends javax.swing.JDialog {

    ModeloM oModeloM;
    int cantidad = 0;

    public frmCantidad(java.awt.Frame parent, boolean modal, ModeloM oM) {
        super(parent, modal);
        initComponents();
        oModeloM = oM;
        txtModelo.setText(oM.getNomModelo());
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        txtCantidad.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();

                // Verificar si la tecla pulsada no es un digito
                if (((caracter < '0')
                        || (caracter > '9'))
                        && (caracter != '\b' /*corresponde a BACK_SPACE*/)) {
                    e.consume();  // ignorar el evento de teclado
                }
            }
        });
        
        txtCantidad.requestFocus();
        configurar();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtModelo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cantidad a Vender");
        setAlwaysOnTop(true);

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 0, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Modelo");

        txtModelo.setEditable(false);
        txtModelo.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        txtModelo.setForeground(new java.awt.Color(0, 0, 102));
        txtModelo.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtModelo.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel4.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 0, 0));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("Cantidad");

        txtCantidad.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        txtCantidad.setForeground(new java.awt.Color(0, 0, 51));
        txtCantidad.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCantidadKeyReleased(evt);
            }
        });

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtModelo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtModelo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        if (!buscarModelo()) {
            ArrayListTableModel temp = (ArrayListTableModel) frmVenta.tbnVenta.getModel();
            String m = " " + oModeloM.getNomModelo();
            String id = "" + oModeloM.getIdmodelo();
            cantidad = Integer.parseInt(txtCantidad.getText());
            if (cantidad <= oModeloM.getStockModelo()) {
                double cant = 0;
                if (cantidad >= 3) {
                    cant = oModeloM.getPrecioMayor();
                } else {
                    cant = oModeloM.getPrecio();
                    cantidad=0;
                }
                //System.out.println(oModeloM.getPrecioMayor());
                Object nuevos[] = {id, m, txtCantidad.getText(), cant, Integer.parseInt(txtCantidad.getText()) * Double.parseDouble(cant + ""), false, oModeloM};
                temp.addRow(nuevos);
                frmVenta.calcularImporte();
            } else {
                dispose();
            }
        }
        frmVenta.calcularImporte();
        frmVenta.tbnModelo.requestFocus();
        this.dispose();
    }//GEN-LAST:event_btnAgregarActionPerformed

    @SuppressWarnings("static-access")
    private void txtCantidadKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER) {
            btnAgregar.requestFocus();
        }
    }//GEN-LAST:event_txtCantidadKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtModelo;
    // End of variables declaration//GEN-END:variables

    private boolean buscarModelo() {
        int filas = frmVenta.tbnVenta.getRowCount();
          //System.out.println("mf"+filas);
        double canti = 0; 
        for (int i = 0; i < filas; i++) {
         //   ModeloM modeloM = (ModeloM) frmVenta.tbnVenta.getValueAt(i, 6);
            //System.out.println("m"+modeloM.getIdmodelo());
            
            if (oModeloM.getIdmodelo() == Integer.parseInt(frmVenta.tbnVenta.getValueAt(i, 0).toString()))  {
                cantidad = Integer.parseInt(txtCantidad.getText()) + (Integer.parseInt(frmVenta.tbnVenta.getValueAt(i, 2).toString()));
                if (cantidad <= oModeloM.getStockModelo()) {
                    frmVenta.tbnVenta.setValueAt(cantidad, i, 2);
                    //System.out.println("m"+cantidad);
                   if (cantidad >= 3) {
                        // System.out.println("m"+modeloM.getPrecioMayor());
                        canti = oModeloM.getPrecioMayor();
                    } else {
                        //System.out.println("me"+modeloM.getPrecio());
                        canti = oModeloM.getPrecio();
                    }
                    frmVenta.tbnVenta.setValueAt((Integer.parseInt(txtCantidad.getText()) + (Integer.parseInt(frmVenta.tbnVenta.getValueAt(i, 2).toString()))) * canti, i, 4);
                    frmVenta.tbnVenta.setValueAt(canti, i, 3);
                    return true;
                } else {
                    dispose();
                    return true;
                }
            }
        }
        return false;
    }
    
    
    private void configurar() {
        
    }
 
}
